package com.pickgliss.ui.controls
{
	import com.pickgliss.ui.ComponentFactory;
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.utils.DisplayUtils;
	import com.pickgliss.utils.ObjectUtils;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	/**
	 * 支持选中与未选中的文本按钮 
	 * @author Herman
	 * 
	 */	
	public class SelectedCheckButton extends SelectedButton
	{
		public static const P_fieldX:String = "fieldX";
		public static const P_fieldY:String = "fieldY";
		public static const P_text:String = "text";
		public static const P_textField:String = "textField";

		public function SelectedCheckButton()
		{
			super();
		}

		protected var _field:DisplayObject;
		protected var _fieldX:Number;
		protected var _fieldY:Number;
		protected var _text:String;
		protected var _textStyle:String;
		
		override public function dispose():void
		{
			if(_field)ObjectUtils.disposeObject(_field);
			_field = null;
			graphics.clear();
			super.dispose();
		}
		/**
		 * 
		 * @param value 文本的X轴
		 * 
		 */		
		public function set fieldX(value:Number):void
		{
			if(_fieldX == value)return;
			_fieldX = value;
			onPropertiesChanged(P_fieldX);
		}
		/**
		 * 
		 * @param value 文本的Y轴
		 * 
		 */		
		public function set fieldY(value:Number):void
		{
			if(_fieldY == value)return;
			_fieldY = value;
			onPropertiesChanged(P_fieldY);
		}
		/**
		 * 
		 * @param value 文本
		 * 
		 */		
		public function set text(value:String):void
		{
			if(_text == value)return;
			_text = value;
			onPropertiesChanged(P_text);
		}
		/**
		 * 
		 * @param field 文本框
		 * 
		 */		
		public function set textField(field:DisplayObject):void
		{
			if(_field == field)return;
			ObjectUtils.disposeObject(_field);
			_field = field;
			onPropertiesChanged(P_textField);
		}
		/**
		 * 
		 * @param stylename 文本框的样式
		 * 
		 */		
		public function set textStyle(stylename:String):void
		{
			if(_textStyle == stylename)return;
			_textStyle = stylename;
			textField = ComponentFactory.Instance.creat(_textStyle);
		}
		
		public function get textWidth():int
		{
			return TextField(_field).textWidth;
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			if(_field)addChild(_field);
		}
		
		protected function drawClickArea():void
		{
			graphics.beginFill(0xff00ff,0);
			graphics.drawRect(0,0,_width,_height);
			graphics.endFill();
		}
		
		override protected function onProppertiesUpdate():void
		{
			super.onProppertiesUpdate();
			if(_changedPropeties[P_fieldX] || _changedPropeties[P_fieldY])
			{
				if(_field)
				{
					_field.x = _fieldX;
					_field.y = _fieldY;
				}
			}
			if(_changedPropeties[P_text])
			{
				if(_field)
				{
					if(_field is TextField)TextField(_field).text = _text;
					_width = _field.x+_field.width;
					_height = Math.max(_field.height,_selectedButton.height);
					drawClickArea();
				}
			}
			if(_changedPropeties[Component.P_height] || _changedPropeties[Component.P_width])
			{
				drawClickArea();
			}
		}
	}
}